package com.srd.phone.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;

import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shengrongdai.srd.R;
import com.srd.phone.adapter.WithdrawRecordAdapter;
import com.srd.phone.bean.WithdrawRecord;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 *  提现记录
 */
public class WithdrawRecordActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2 {


    private PullToRefreshListView mListView;
    private WithdrawRecordAdapter adapter = null;
    private List<WithdrawRecord> mList = new ArrayList<WithdrawRecord>(10);
    private UtilPresenter utilPresenter = null;
    private int currentPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_record);

        initView();
        initData();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void initView() {
        tv_common_title.setText("提现记录");

        mListView = (PullToRefreshListView)findViewById(R.id.listview);
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        mListView.setOnRefreshListener(this);
        mListView.setScrollingWhileRefreshingEnabled(true);
        adapter = new WithdrawRecordAdapter(this, mList);
        mListView.setAdapter(adapter);

    }

    public void initData() {
        showWaitingDialog();
        presenter = new UtilPresenter(requestQueue, this);
        utilPresenter = (UtilPresenter)presenter;
        setUrlArrs(Constants.WITHDRAW_RECORD);

        getData();
    }

    private void getData(){
        utilPresenter.requestData(Constants.WITHDRAW_RECORD, currentPage);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        complete();
        if(Constants.WITHDRAW_RECORD.equals(tag)){
            if(currentPage == 1) mList.clear();
            List<WithdrawRecord> list = WithdrawRecord.parseData(data);
            if(list == null || list.size() == 0){
                toastShow("没有更多数据~");
                return;
            }
            currentPage += 1;
            mList.addAll(list);
            adapter.setData(mList);
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        complete();
    }

    private void complete(){
        cancelWaitingDialog();
        mListView.onRefreshComplete();
    }

    @Override
    protected void onDestroy() {
        complete();
        mList.clear();
        mList = null;
        super.onDestroy();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        currentPage = 1;
        getData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        getData();
    }
}